//
//  StructionViewController.m
//  Drivinglicense
//
//  Created by HappyMilk 02 on 2017/6/29.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "StructionViewController.h"
#import "LoginHeader.h"
#import "MJRequestServer.h"
#import "QuestionModel.h"
#import <SVProgressHUD.h>
#import "TestViewController.h"
#import "FormalTestController.h"
#import "MJUserModel.h"
#import <UIButton+WebCache.h>
#import "MJMyMsgController.h"
#import "LoginViewController.h"

@interface StructionViewController ()

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgv_height;

@property (weak, nonatomic) IBOutlet UIButton *btn_user_head;
@property (weak, nonatomic) IBOutlet UILabel *lab_instruction;

@property (weak, nonatomic) IBOutlet UIView *view_bg;

@property (nonatomic ,strong) MJUserModel *userModel;

@end

@implementation StructionViewController

- (IBAction)popvc:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [MJUserModel registerSubclass];
    if ([MPWeChat stander].m_isLogin) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"first"]) {
            [self findUserData];
        }else{
            [self createUserModel];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if (IS_IPAD) {
        self.imgv_height.constant = 450;
    }
    self.view_bg.layer.cornerRadius = 8;
    self.view_bg.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    self.view_bg.layer.shadowOffset = CGSizeMake(0,4);;
    self.view_bg.layer.shadowRadius = 4;
    self.view_bg.layer.shadowOpacity = 0.8;
    self.btn_user_head.layer.masksToBounds = YES;
    self.btn_user_head.layer.cornerRadius = 33;
}

- (IBAction)loginAction:(UIButton *)sender {
    
    if ([MPWeChat stander].m_isLogin) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mine" bundle:nil];
        MJMyMsgController *vc = [sb instantiateViewControllerWithIdentifier:@"MJMyMsgController"];
        vc.userModel = _userModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        LoginViewController *vc = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
        [self presentViewController:vc animated:YES completion:^{
            
        }];
    }
}

- (void)findUserData{
    
    AVQuery *query = [AVQuery queryWithClassName:@"User_list"];
    NSString *objcid = [[NSUserDefaults standardUserDefaults] objectForKey:@"first"];
    [query getObjectInBackgroundWithId:objcid block:^(AVObject * _Nullable object, NSError * _Nullable error) {
        MJUserModel *model = (MJUserModel *)object;
        _userModel = model;
        [self.btn_user_head sd_setBackgroundImageWithURL:[NSURL URLWithString:model.head_img.url] forState:UIControlStateNormal];
        self.lab_instruction.text = model.name;
    }];
}
- (void)createUserModel{
    
    MJUserModel *model = [MJUserModel object];
    NSLog(@"------ %@",model.objectId);
    model.user_id = @"";
    model.name = @"李某某";
    model.phoneNum = @"暂无";
    model.sex = @"男";
    model.adrs = @"暂无";
    model.adrs_now = @"暂无";
    model.birthday = @"暂无";
    model.height = @"暂无";
    model.school_record = @"暂无";
    model.enmotion = @"暂无";
    model.signature = @"暂无";
    UIImage *image = [UIImage imageNamed:@"user_head_df"];
    NSData *imageData=UIImageJPEGRepresentation(image,0.6);
    AVFile *file = [AVFile fileWithName:@"user_head_df" data:imageData];
    model.head_img = file;
    [model saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        NSLog(@"------ %@",model.objectId);
        [[NSUserDefaults standardUserDefaults] setObject:model.objectId forKey:@"first"];
        [self.btn_user_head setBackgroundImage:image forState:UIControlStateNormal];
        self.lab_instruction.text = model.name;
    }];
    _userModel = model;
}

- (IBAction)StartAnswerQuestiones:(UIButton *)sender {
    if (IS_IPAD) {
        
    }
    [SVProgressHUD show];
    NSDictionary *dic = @{@"type":@"C1",
                          @"subject":@"1",
                          @"pagesize":@"100",
                          @"pagenum":@"1",
                          @"sort":@"rand",
                          @"appkey":@"63a288a68764b5e2"};
    [MJRequestServer Post:dic success:^(id sender) {
        
        NSMutableArray *muarr = [QuestionModel mj_objectArrayWithKeyValuesArray:sender];
        
        [SVProgressHUD dismiss];
        
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        FormalTestController *vc = [sb instantiateViewControllerWithIdentifier:@"FormalTestController"];
        vc.testArray = muarr;
        [self.navigationController pushViewController:vc animated:YES];
        
    } andFailure:^(id sender) {
        [SVProgressHUD showInfoWithStatus:@"请再次尝试"];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
